package cate.game.play.skill.passive.fairy;

import cate.common.table.d.GD;
import cate.common.table.d.GDAttr;
import cate.common.table.d.GDSkill;
import cate.game.attr.FightAttr;
import cate.game.play.config.PlayCfgArgs;
import cate.game.play.fighter.Fighter;
import cate.game.play.proce.action.ctx.SkillActionCtx;
import cate.game.play.proce.action.ctx.TimeActionCtx;
import cate.game.play.proce.move.MoveCtx;
import cate.game.play.skill.effect.SkillEffect;
import cate.game.play.skill.passive.PassiveHandler;

/**
 * 一速时附加的伤害额外提升（6%+速度差系数），额外附加10%暴击伤害，
 *  非一速时，增加(6%+速度差系数)对额外攻击目标的伤害，双方速度差越高，速度差系数越高最高不超过30%
 */
public class 箭竹饭灵器PH extends PassiveHandler {


	private FightAttr 固定属性;

	private double 速度差最大值;

	private FightAttr 一速属性;


	//固定属性=23:600&速度差最大值=3000&一速属性=12:1000
	@Override
	public void parse(String code) {
		PlayCfgArgs args = new PlayCfgArgs(code);
		固定属性 = new FightAttr(args.get("固定属性"));
		速度差最大值 = args.getWDouble("速度差最大值", 0d);
		一速属性 = new FightAttr(args.get("一速属性"));
	}

	@Override
	public void beforeOwnerMove(MoveCtx move, TimeActionCtx action) {
		if (move.getTurn().getTeamOrder() == 1) {
			skill.owner.attr.addMoveAttr(固定属性);
			skill.owner.attr.addMoveAttr(一速属性);
		}
	}


	@Override
	public void afterSelectTargets(SkillActionCtx action, SkillEffect effect) {
		if (!action.getSkill().cfg.isEffectType(GDSkill.EffectType.伤害)) {
			return;
		}
		int teamOrder = action.getTurn().getTeamOrder();
		if (teamOrder <= 0) {
			return;
		}
		if (teamOrder == 1) {
			Fighter target = action.getTargets().get(0);
			effect.getTargetCtx(target.getPid()).addActorAttr(calSpeedAttr(target));
		} else {
			for (Fighter target : action.getExTargets()) {
				effect.getTargetCtx(target.getPid()).addActorAttr(固定属性);
				effect.getTargetCtx(target.getPid()).addActorAttr(calSpeedAttr(target));
			}
		}
	}

	private FightAttr calSpeedAttr(Fighter target) {
		FightAttr speedAttr = new FightAttr();
		double tarSpeed = target.attr.total().speedTotal();
		if (tarSpeed > 0) {
			double diff = skill.owner.attr.total().speedTotal() / tarSpeed * GD.W_10000_0 - GD.W_10000;
			if (diff > 0) {
				speedAttr.add(GDAttr.DAMAGE_ADD, Math.min(diff, 速度差最大值));
			}
		}
		return speedAttr;
	}
}
